import { useDepartmentStore } from '@/stores/department.store.ts'
import { storeToRefs } from 'pinia'
import type { DepartmentInfo, Page } from '@/hooks/department.hook.type.ts'
import { toRefs, unref } from 'vue'
import { DialogType, type SearchForm } from '@/stores/department.store.type.ts'
import type { Result } from '@/utils/http.ts'
import http from '@/utils/http.ts'

export const useDepartmentSearch = () => {
  const store = useDepartmentStore()
  const { search } = storeToRefs(store)
  const { form } = toRefs(search.value)

  function clearSearchForm() {
    form.value = {} as SearchForm
  }

  return {
    clearSearchForm
  }
}

export const useDepartmentTable = () => {
  const { table } = storeToRefs(useDepartmentStore())
  const {
    height,
    list,
    total,
    $name,
    $location,
    $pageSize,
    $currentPage,
    currentDepartment
  } = toRefs(table.value)

  async function fetchDataForList() {
    const params = {
      name: unref($name),
      location: unref($location),
      currentPage: unref($currentPage),
      pageSize: unref($pageSize)
    }

    const res: Result<Page<DepartmentInfo>> = await http.get('/api/department/page', params)
    if (res && res.code) {
      list.value = res.data.records
      total.value = res.data.total

      height.value = window.innerHeight - 230
    }
  }

  function chooseCurrentDepartment(dept: DepartmentInfo) {
    currentDepartment.value = {
      id: dept.id,
      name: dept.name,
      location: dept.location
    }
  }

  function deleteCurrentDepartment(id: number): Promise<Result<null>> {
    const params = {
      id: id
    }
    return http.post(`/api/department/${id}/delete`, params)
  }


  return { fetchDataForList, chooseCurrentDepartment, deleteCurrentDepartment }
}

export const useDepartmentPagination = () => {
  const { pagination } = storeToRefs(useDepartmentStore())
  const {} = toRefs(pagination.value)
}

export const useDepartmentDialog = () => {
  const { dialog } = storeToRefs(useDepartmentStore())
  const {
    title,
    visible,
    form,
    $currentDepartment,
    type
  } = toRefs(dialog.value)

  function clearDialogForm() {
    form.value.id = 0
    form.value.name = ''
    form.value.location = ''
  }

  function closeDialog() {
    clearDialogForm()
    visible.value = false
  }

  function showDialogForAdd() {
    type.value = DialogType.Add
    title.value = '新增'
    visible.value = true
  }

  function showDialogForUpdate() {
    type.value = DialogType.Update
    title.value = '修改'
    form.value = unref($currentDepartment)
    visible.value = true
  }

  function submitFormForAdd(): Promise<Result<null>> {
    const params = {
      name: unref(form).name,
      location: unref(form).location
    }
    return http.post('/api/department/put', params)
  }

  function submitFormForUpdate(): Promise<Result<null>> {
    const params = {
      id: unref(form).id,
      name: unref(form).name,
      location: unref(form).location
    }
    return http.post(`/api/department/${params.id}/post`, params)
  }

  return {
    clearDialogForm, closeDialog,
    showDialogForAdd, showDialogForUpdate,
    submitFormForAdd, submitFormForUpdate
  }
}
